<template>
    <div>
      <h1>管理员信息</h1>
        <el-button type="primary" @click="dialogVisible = true">添加管理员</el-button>
    <!-- 对话框 -->
    <el-dialog
      title="添加管理员"
      :visible.sync="dialogVisible"
      width="30%"
      @close="handleClose">
      <!-- 表单内容 -->
      <el-form :model="adminForm" label-width="120px">
        <el-form-item label="ID">
          <el-input v-model="adminForm.id"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="adminForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="adminForm.mypassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="confirmAdd">确定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table :data="student" style="width: 80%">
    <el-table-column prop="id" label="ID" width="180">
      <template #default="{ row }">
        <el-input v-if="row.isEdit" v-model="row.id"></el-input>
        <span v-else class="text-table">{{ row.id }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="name" label="用户名" width="180">
      <template #default="{ row }">
        <el-input v-if="row.isEdit" v-model="row.name"></el-input>
        <span v-else class="text-table">{{ row.name }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="mypassword" label="密码" width="180">
      <template #default="{ row }">
        <el-input v-if="row.isEdit" v-model="row.mypassword"></el-input>
        <span v-else class="text-table">{{ row.mypassword }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row, $index }">
        <el-button v-if="!row.isEdit" type="primary" @click="edit($index)">点击修改</el-button>
        <el-button v-else type="primary" @click="saveAdmin($index)">保存</el-button>
        <el-button type="danger" @click="deleteAdmin($index)">删除管理员</el-button>
      </template>
    </el-table-column>
  </el-table>
    </div>
  </template>
  
  <script>
    import axios from 'axios';
  export default {
      name: 'adminInfo',
      data(){
          return {
              student:[],
              issave:false,
              dialogVisible: false,
      adminForm: {
        id: '',
        name: '',
        mypassword: ''
      }
          }
      },
      //写一个方法在刚到页面的时候就加载学生列表
      created(){
          this.getStudents()
      },
      methods: {
        handleClose() {
      this.dialogVisible = false;
    },
    confirmAdd() {
      // 在这里处理表单提交逻辑
      axios.post('api/admin/addadmin', this.adminForm)
      .then(response =>{
        if(response.data.code === 1){
          alert(response.data.data);
        }else{
          alert(response.data.msg);
        }
      }).catch(error =>{
        console.error(error);
      })
      this.dialogVisible = false;
    },
          getStudents(){
              axios.get('api/admin/admininfo')
          .then(response => {
            const list = response.data.data;
            for(let i=0;i<list.length;i++){
              const item = {
                id:list[i].id,
                name:list[i].name,
                mypassword:list[i].mypassword,
                isEdit:false,
              }
              this.student.push(item);
            }
          
             }).catch(error => {
               console.error(error);
             });
          },
          edit(index){
            this.issave = true;
            this.student[index].isEdit = true;
          },
          deleteAdmin(index){
            const isDelete = confirm('确认删除吗？');
            if(!isDelete){
              return;
            }
            const id = this.student[index].id;
            axios.post('api/admin/deleteadmin', {id:id})
            .then(response => {
              alert(response.data.data);
            })
            .catch(error => {
              if(error.response.data===-3){
        alert('密钥失效，请联系管理员')
      }else if(error.response.data===-1){
        alert('用户名不存在或密码错误')
      }else if(error.response.data===-2){
        alert('只能删除当前自己的管理员账号')
      }
            });
          },
    saveAdmin(index){
      axios.post('api/admin/editadmin', this.student[index])
        .then(response => {
             if(response.data.code === 0){
              alert(response.data.msg)
             }else{
              this.issave = false;
           this.student[index].isEdit = false;
             }
           }).catch(error => {
             console.error(error);
           });          
    }
      }
  }
  </script>
  
  <style>
.text-table {
  font-size: 18px;
  font-weight: bold;
}
  </style>